var gulp = require('gulp');
var less = require('gulp-less');
var cssmin = require('gulp-minify-css');
// 调试的时候用
var del = require('del');
var uglify = require('gulp-uglify');
var minihtml = require('gulp-htmlmin');
var babel = require('gulp-babel');

gulp.task('clean:prod', function(done) {
    del(['dist'], done());
});
// 输出图片
gulp.task('packimg:prod', function() {
    return gulp.src('src/img/*.{png,jepg,gif,ico}')
        .pipe(gulp.dest('dist/img'));
});
// 压缩html
gulp.task('minihtml:prod', function() {
    var option = {
        removeComments: true, //清除HTML注释
        collapseWhitespace: true, //压缩HTML
        collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input checked />
        removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true, //删除<style>和<link>的type="text/css"
        minifyJS: true, //压缩页面内部JS
        minifyCSS: true //压缩页面内部CSS
    }
    return gulp.src('src/*.html')
        .pipe(minihtml(option))
        .pipe(gulp.dest('dist'))
});
// 编译es6
gulp.task('convert:prod', function() {
    return gulp.src('src/es6/*.js')
        // 这里编译规则要用对象
        .pipe(babel({
            "presets": ["@babel/preset-env"]
        }))
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'));
});
// 删除最新编译前的文件
gulp.task('clean:prod', function(done) {
    del(['dist'], done());
    // 打包时删除开发环境时 的编译
    del(['src/css'], done());
    del(['src/js'], done());
});
// 编译less并压缩导出任务
gulp.task('testless:prod', function() {
    return gulp.src('src/less/*.less')
        .pipe(less()) //编译less
        .pipe(cssmin()) //压缩编译后的css
        .pipe(gulp.dest('dist/css')); //导出文件路径
});